package com.example.educational.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educational.controller.utils.BusinessException;
import com.example.educational.controller.utils.Code;
import com.example.educational.entity.Attachment;
import com.example.educational.entity.NoticeReceived;
import com.example.educational.mapper.NoticeReceivedMapper;
import com.example.educational.service.NoticeReceivedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author MySoul
 * @description 针对表【edu_notice_received】的数据库操作Service实现
 * @createDate 2023-05-19 08:43:12
 */
@Service
public class NoticeReceivedServiceImpl extends ServiceImpl<NoticeReceivedMapper, NoticeReceived>
        implements NoticeReceivedService {
    @Autowired
    NoticeReceivedMapper noticeReceivedMapper;


    @Override
    public Boolean savaNoticeReceived(NoticeReceived noticeReceived) {
        System.out.println(noticeReceived);
        int result = 0;
        String releaseDate = noticeReceived.getReleaseDate();
        String title = noticeReceived.getTitle();
        String category = noticeReceived.getCategory();
        String imgSrc = noticeReceived.getImgSrc();
        ArrayList<Attachment> fileSrc = noticeReceived.getFileSrc();
        String jsonStr = JSONUtil.toJsonStr(fileSrc);
        DateTime dateTime = DateUtil.parse(releaseDate);
        long time = dateTime.getTime();
        String id = String.valueOf(time);
        try {
            result = noticeReceivedMapper.insertNoticeReceived(id, title, category, releaseDate, imgSrc, jsonStr);
        } catch (DuplicateKeyException e) {
            return Boolean.FALSE;
        }
        return result > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public NoticeReceived checkReceiveNotice(String jsonArrObj) {
        // 1.检查数据完整性
        JSONObject parsedObj = JSONUtil.parseObj(jsonArrObj);
        NoticeReceived noticeReceived = parsedObj.toBean(NoticeReceived.class);
        // 2.检查数据有效性
        DateTime now = DateTime.now();
        String releaseDate = noticeReceived.getReleaseDate();
        if (releaseDate == null)
            throw new BusinessException(Code.err, "服务器异常，请勿非法访问");
        DateTime dateTime = DateUtil.parse(releaseDate);
        boolean after = now.isAfter(dateTime);
        if (after != Boolean.TRUE)
            throw new BusinessException(Code.err, "服务器异常，时间校验失败");
        return noticeReceived;
    }

    @Override
    public NoticeReceived getReceiveNoticed(String taskId) {
        NoticeReceived noticeReceived = noticeReceivedMapper.selectById(taskId);
        if (ObjectUtil.isNotNull(noticeReceived)) {
            String jsonStr = noticeReceivedMapper.selectFileSrcById(taskId);
            noticeReceived.setName("通知公告");
            ArrayList<Attachment> fileSrc = new ArrayList<>();
            JSONArray objects = JSONUtil.parseArray(jsonStr);
            for (int i = 0; i < objects.size(); i++) {
                Attachment attachment = JSONUtil.toBean(String.valueOf(objects.get(i)), Attachment.class);
                fileSrc.add(attachment);
            }
            noticeReceived.setFileSrc(fileSrc);
        }
        return noticeReceived;
    }
}




